// 控制器服务模块入口

import { ElMention, ElMessage } from "element-plus";
import Nifi from "../../nifi";
import { h } from "vue";

// 
import TabsElem from "./tabs.vue"
import {t} from "../../i18n/index"

export default (nifi: Nifi) => {


    // 打开控制器列表
    // 跳转到指定控制器
    // 新建控制器 并跳转
    // 关闭控制器列表
    // 


    // API
    // 获取控制器列表
    const get = async (id?: string) => {
        const url = id ? `/nifi-api/flow/process-groups/${id}/controller-services?uiOnly=true` : `/nifi-api/flow/controller/controller-services?uiOnly=true`
        try {
            const res = await nifi.api.get(url)
            return res.controllerServices as any[];
        } catch (error) {
            ElMessage.error(t('cs-index-get-failed',{error}));
            console.error(error)
            return [];
        }
    }
    // 创建控制器服务
    const add = async (data: any, groupId = nifi.group.id) => {
        const url = `/nifi-api/process-groups/${groupId}/controller-services`;
        const t = { ...data, disconnectedNodeAcknowledged: false, revision: { version: 0, clientId: nifi.flow.clientId } }
        try {
            return await nifi.api.post(url, t)
        } catch (error) {
            // ElMessage.error('创建控制器服务失败 :: ' + error);
            ElMessage.error(t('cs-index-new-failed',{error}));
            console.error(error)
            return false;
        }
    }

    // 更新控制器服务
    const update = async (data: any) => {
        const id = data.component.id;
        const url = `/nifi-api/controller-services/${id}`;
        try {
            return await nifi.api.put(url, data)
        } catch (error) {
            // ElMessage.error('更新控制器服务失败 :: ' + error);
            ElMessage.error(t('cs-index-update-failed',{error}));
            console.error(error)
            return false;
        }
    }

    // 删除控制器
    const remove = async (data: any) => {
        const id = data.component.id;
        const version = data.revision.version;
        const clientId = nifi.flow.clientId;
        const url = `/nifi-api/controller-services/${id}?version=${version}&clientId=${clientId}&disconnectedNodeAcknowledged=false`
        try {
            return await nifi.api.delete(url)
        } catch (error) {
            // ElMessage.error('删除控制器失败 :: ' + error);
            ElMessage.error(t('cs-index-delete-failed',{error}));
            console.error(error)
            return false;
        }
    }
    // 获取控制器服务详情
    const detail = async (id: string) => {
        const url = `/nifi-api/controller-services/${id}?uiOnly=true`
        try {
            return nifi.api.get(url);
        } catch (error) {
            // ElMessage.error('获取控制器服务详情失败 :: ' + error);
            ElMessage.error(t('cs-index-get-detail-failed',{error}));
            console.error(error)
            return false;
        }
    }
    // 新增控制器服务属性
    const addProperty = async (id: string, name: string, sensitive: boolean) => {
        const url = `/nifi-api/controller-services/${id}/descriptors?propertyName=${name}&sensitive=${sensitive}`
        try {
            return nifi.api.get(url);
        } catch (error) {
            // ElMessage.error('新增控制器服务属性失败 :: ' + error);
            ElMessage.error(t('cs-index-add-prop-failed',{error}));
            console.error(error)
            return false;
        }
    }

    // 验证控制器属性
    const validateProperty = async (id: string) => {
        // 分析
        let url = `/nifi-api/controller-services/${id}/config/analysis`
        let res: any;
        res = await nifi.api.post(url, { configurationAnalysis: { componentId: id, properties: {} } })
        // 验证
        url = `/nifi-api/controller-services/${id}/config/verification-requests`
        res = await nifi.api.post(url, { request: { attributes: {}, componentId: id, properties: {} } })
        // 轮询结果
        url = `/nifi-api/controller-services/${id}/config/verification-requests/${res.request.requestId}`;
        do {
            res = await nifi.api.get(url)
            await new Promise((resolve) => setTimeout(() => resolve(true), 500));
        }
        while (res.request.complete == false)
        // 关闭
        res = await nifi.api.delete(url)
        return res;
    }

    // 启用所有的控制器服务
    const enableAll = async (groupId = nifi.group.id) => {
        const url = `/nifi-api/flow/process-groups/${groupId}/controller-services`
        //
        const data = {"id":groupId,"state":"ENABLED","disconnectedNodeAcknowledged":false};
        try {
            await nifi.api.put(url,data)
            ElMessage.success(t('cs-index-enable-all'));
        } catch (error) {
            // ElMessage.error('启用所有的控制器服务失败 :: ' + error);
            ElMessage.error(t('cs-index-enable-failed',{error}));
            console.error(error)
            return false;
        }
    }

    // 禁用所有的控制器服务
    const disableAll = async (groupId = nifi.group.id) => {
        const url = `/nifi-api/flow/process-groups/${groupId}/controller-services`
        //
        const data = {"id":groupId,"state":"DISABLED","disconnectedNodeAcknowledged":false};
        //
        try {
            await nifi.api.put(url,data)
            ElMessage.success(t('cs-index-disable-all'));
        } catch (error) {
            // ElMessage.error('禁用所有的控制器服务失败 :: ' + error);
            ElMessage.error(t('cs-index-disable-failed',{error}));
            console.error(error)
            return false;
        }

    }


    // 显示控制器服务列表
    const open = (groupId?:string,id?:string) => {
        return nifi.box.dialog((box) => h(TabsElem,{nifi,groupId,id,onClose:() => box.close()}),{title:t('cs-index-dialog-title'),showFooter:false})
    }


    return {
        get,
        add,
        update,
        remove,
        detail,
        addProperty,
        validateProperty,
        enableAll,
        disableAll,
        //
        open,
    }
}